package com.sen.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sen.constant.StockConstant;
import com.sen.entity.SellheadEntity;
import com.sen.entity.StockheadEntity;
import com.sen.service.StockheadService;
import com.sen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 进货表头表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-08 17:54:40
 */
@RestController
@RequestMapping("stockhead")
public class StockheadController {
    @Autowired
    private StockheadService stockheadService;

    /**
     * 查看所有销售订单
     * @param stockheadMap 采购订单头实体类
     * @return
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String,String> stockheadMap) {
        System.out.println("map:"+stockheadMap);
        Page<StockheadEntity> stockheadEntityPage = stockheadService.queryAll(stockheadMap);
        return R.ok().put("stockHeads", stockheadEntityPage);
//        return R.ok();
    }

    /**
     * 订单驳回
     * @param stockheadEntity
     * @return
     */
    @PostMapping("/stockNo")
    public R stockNo(String stockheadEntity,@RequestParam Integer eid){
        StockheadEntity stockheadEntity1 = JSON.parseObject(stockheadEntity, StockheadEntity.class);
        stockheadEntity1.setEid(eid);
        stockheadEntity1.setState(StockConstant.StateEnum.STATE_SEND_BACK.getState());
        stockheadService.updateById(stockheadEntity1);
        return R.ok();
    }

    /**
     * 订单通过
     * @param hid 订单头id
     * @return
     */
    @GetMapping("/stockYes/{hid}/{eid}")
    public R stockYes(@PathVariable Integer hid,@PathVariable Integer eid){
        StockheadEntity stockheadEntity = new StockheadEntity();
        stockheadEntity.setHid(hid);
        stockheadEntity.setEid(eid);
        stockheadEntity.setState(StockConstant.StateEnum.STATE_NO_PUT_STORAGE.getState());
        stockheadService.updateById(stockheadEntity);

        return R.ok();
    }

    /**
     * 驳回后重新提交的采购信息
     * @param stockheadData
     * @return
     */
    @PostMapping("/stockAnew")
    public R stockAnew(@RequestParam String stockheadData){
        StockheadEntity stockheadEntities = JSON.parseObject(stockheadData, StockheadEntity.class);
        //重新生成订单
        stockheadService.anewStock(stockheadEntities);
        return R.ok();
    }

    /**
     * 图表数据
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/getDataSta/{begin}/{end}")
    public R getDataSta(@PathVariable String begin,@PathVariable String end) {
        Map<String, Object> map = stockheadService.getDataSta(begin, end);

        return R.ok().put("staMap", map);
    }

}
